//
//  DYTabBarController.m
//  Excellence
//
//  Created by 帝云科技 on 2017/6/16.
//  Copyright © 2017年 diyunkeji. All rights reserved.
//

#import "DYTabBarController.h"
#import "DYLoginVC.h"
#import "DYNavigationController.h"

@interface DYTabBarController ()<
UITabBarControllerDelegate>

@property (nonatomic, strong) UIButton *operationButton;

@end

static NSString *const _image_highlighted = @"_highlighted";

static NSString *const _tabbar_image_1 = @"tabbar_home";
static NSString *const _tabbar_image_2 = @"tabbar_shop";
static NSString *const _tabbar_image_3 = @"tabbar_message";
static NSString *const _tabbar_image_4 = @"tabbar_mine";

static NSString *const _tabbar_vc_1 = @"DYHomeVC";
static NSString *const _tabbar_vc_2 = @"DYShopVC";
static NSString *const _tabbar_vc_3 = @"DYMessageVC";
static NSString *const _tabbar_vc_4 = @"DYMineVC";

static NSString *const _tabbar_title_1 = @"首页";
static NSString *const _tabbar_title_2 = @"商铺";
static NSString *const _tabbar_title_3 = @"消息";
static NSString *const _tabbar_title_4 = @"我的";

static NSString *const _navigation_vc = @"DYNavigationController";

@implementation DYTabBarController

+ (void)initialize
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    attrs[NSForegroundColorAttributeName] = [UIColor xhq_hexb9];
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor xhq_base];
    
    UITabBarItem *items = [UITabBarItem appearance];
    [items setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [items setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    UITabBar *tabbar = [UITabBar appearance];
    tabbar.translucent = NO;
    tabbar.opaque = NO;
    [tabbar setBarTintColor:[UIColor whiteColor]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
//    [self.tabBar bringSubviewToFront:self.operationButton];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addChildViewControllers];
    self.delegate = self;
}

- (void)addChildViewControllers
{
    [self addChildViewControllerClassName:_tabbar_vc_1 title:_tabbar_title_1 imageName:_tabbar_image_1];
    [self addChildViewControllerClassName:_tabbar_vc_2 title:_tabbar_title_2 imageName:_tabbar_image_2];
    [self addChildViewControllerClassName:_tabbar_vc_3 title:_tabbar_title_3 imageName:_tabbar_image_3];
    [self addChildViewControllerClassName:_tabbar_vc_4 title:_tabbar_title_4 imageName:_tabbar_image_4];
}

- (void)addChildViewControllerClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName
{
    UIViewController *viewController = [[NSClassFromString(className) alloc]init];
    
    Class navClass = NSClassFromString(_navigation_vc);
    UINavigationController *nav = [[navClass alloc]initWithRootViewController:viewController];
    viewController.title = title;
    viewController.view.backgroundColor = XHQHexColor(0xFBFBFB);
    viewController.tabBarItem.image = [[UIImage imageNamed:imageName]
                                       imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:_image_highlighted]]
                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];
}

- (void)addCenterOperationButton
{
    UIButton *operationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [operationButton setImage:[UIImage imageNamed:_tabbar_image_3] forState:UIControlStateNormal];
    [operationButton setBackgroundColor:[UIColor xhq_base]];
    [operationButton setTitle:@"" forState:UIControlStateNormal];
    [operationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [operationButton setXhqFont:[UIFont xhq_font12]];
    [operationButton addTarget:self action:@selector(operationButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [operationButton.imageView xhq_setAspectFitContentMode];
    self.operationButton = operationButton;
    [self.tabBar addSubview:operationButton];
    [self.tabBar bringSubviewToFront:operationButton];
    
    CGFloat width = (self.tabBar.xhq_width - (self.tabBar.xhq_height - 2)) / 2;
    operationButton.frame = CGRectInset(self.tabBar.bounds, width, 2);
    operationButton.xhq_width = operationButton.xhq_height;
    operationButton.layer.cornerRadius = operationButton.xhq_width / 2.0;
    operationButton.layer.masksToBounds = YES;
    
    operationButton.titleEdgeInsets = UIEdgeInsetsMake(operationButton.imageView.xhq_height + 5, -operationButton.imageView.xhq_width, 0, 0);
    operationButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 12, -operationButton.titleLabel.xhq_width);
    operationButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}

- (void)operationButtonClick:(UIButton *)sender
{
    Class class = NSClassFromString(_tabbar_vc_3);
    UIViewController *extend = [[class alloc]init];
    [self presentViewController:extend animated:NO completion:nil];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSInteger selectedIndex = [tabBarController.viewControllers indexOfObject:viewController];
//    BOOL isLogin = [[DYAppContext sharedDYAppContext] isLogin];
//    if (selectedIndex != 0 && !isLogin) {
//        [[DYAppContext sharedDYAppContext] showLoginInViewController:self completion:nil];
//        return NO;
//    }
//    if (selectedIndex==0) {
//        DYLoginVC *vc=[[DYLoginVC alloc]init];
//        DYNavigationController *nav=[[DYNavigationController alloc]initWithRootViewController:vc];
//        [self presentViewController:nav animated:YES completion:nil];
//         return NO;
//    }
    return YES;
}

@end
